package member.base.action;

import com.google.zxing.WriterException;
import m.common.action.Action;
import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.server.web.res.ContentType;
import m.system.exception.MException;
import m.system.util.DictionaryUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import member.base.model.BusinessInfo;
import member.base.model.UserInfo;
import member.base.service.MenuInfoService;
import member.base.service.PageService;
import member.base.util.ImageUtil;
import member.base.util.PowerUtil;

import java.sql.SQLException;

@ActionRequest(name = "action/base/page",description = "页面公共接口")
public class PageAction extends Action {
    @RequestMethod(description = "查询字典所有值")
    public JSONMessage dict(@Param("type") String type) throws MException {
        return JSONMessage.success("")
            .push("map", DictionaryUtil.get(type))
            .push("mapString", ObjectUtil.toString(DictionaryUtil.get(type)).replaceAll("\\{","").replaceAll("}","").replaceAll("\"",""));
    }
    @RequestMethod(description = "信息")
    public JSONMessage info() throws Exception {
        UserInfo user=PowerUtil.getUser(false);
        BusinessInfo business=PowerUtil.getBusiness(false);
        return JSONMessage.success("")
            .push("user", user)
            .push("business",business)
            .push("store",PowerUtil.getStore(false))
            .push("app",
                new JSONMessage()
                    .push("title",null!=business?business.getName():"会员管理系统")
            );
    }
    @RequestMethod(description = "菜单")
    public JSONMessage menus() throws Exception {
        return JSONMessage.success("")
            .push("menus",getService(MenuInfoService.class).getMenus(PowerUtil.getUser(false)));
    }
    @RequestMethod(description = "首页数据")
    public JSONMessage index() throws Exception {
        PageService service=getService(PageService.class);
        String storeOid=PowerUtil.getStore().getOid();
        return JSONMessage.success("")
            .push("data",new JSONMessage()
                .push("PowerStore",PowerUtil.hasPower(UserInfo.PowerStore))
                .push("PowerManage",PowerUtil.hasPower(UserInfo.PowerManage))
                .push("order",service.getOrder(storeOid))
                .push("recharge",service.getRecharge(storeOid))
                .push("month",service.getMonth(storeOid))
            );
    }
    @RequestMethod(description = "返回二维码")
    public void qrcode(@Param("txt") String txt) throws WriterException, MException {
        getResponse().outImage(ImageUtil.toQRCode(txt,300),ContentType.PNG);
    }
}
